package com.tearreview.controller;

import com.github.pagehelper.PageInfo;
import com.tearreview.pojo.User;
import com.tearreview.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
//CRUD就叫增删改查
@Controller
public class UserController {

@Autowired
    UserService userService;

@RequestMapping("/index")
    public String  index(@RequestParam(defaultValue = "1")Integer pageNum,
                         @RequestParam(defaultValue = "10")Integer pageSize,
                         Model model){
    //PageSize  指最多显示多少数据
    List<User >users=userService.findAllByPage(pageNum,pageSize);
    model.addAttribute("users",users);
    PageInfo pageInfo=new PageInfo(users,pageSize);
    model.addAttribute("pageInfo",pageInfo);
    return "index";
}

//这个是测试
@RequestMapping("/user")
public String   user(Model model){
    List<User> lists=userService.findAll();
    model.addAttribute("users",lists);
    return "index";
}

@RequestMapping("/")
    public String  indexME(){
    return  "redirect:/index";
}



@RequestMapping("/ToUpdate")
    public String  goUpdate(int id,Model model){
    User user=userService.findById(id);
    model.addAttribute("user",user);
    return  "update";
}
@RequestMapping("/ToAdd")
public String  ToAdd(){
    return  "add";
}
//删除 最简单的 随便写一个
@RequestMapping("/Delete")
public String  delete(Integer id){ //这个是前端名命名的参数啊!!!
    userService.delete(id);
    return  "redirect:/index";
}

@RequestMapping("edit")
    public String edit(User user){
   //天坑  整数型才可以接收值SSM中，前端搜索条件为空时，value为“”，
    // 传值时无法转换为int，会导致加载controller之前就报错，
    // 故应把属性值得类型改为Integer，即可接收到null
    userService.update(user);
    return "redirect:/index";
}

@RequestMapping("/add")
    public String add(User user){
    userService.add(user);
    return  "redirect:/index";
}

}





